package org.liveboardgames.agricola.control.delegates.mock
{
	import mx.collections.ArrayCollection;
	import mx.collections.ListCollectionView;
	
	import org.liveboardgames.agricola.control.delegates.helper.GameHelper;
	import org.liveboardgames.agricola.control.delegates.mock.cards.ActionCardFactory;
	import org.liveboardgames.agricola.control.delegates.mock.cards.majorimprovements.MajorImprovementCards;
	import org.liveboardgames.agricola.model.enums.BoardTileTypeEnum;
	import org.liveboardgames.agricola.model.enums.GameTypeEnum;
	import org.liveboardgames.agricola.model.enums.ResourceTypeEnum;
	import org.liveboardgames.agricola.vo.Game;
	import org.liveboardgames.agricola.vo.board.BoardTile;
	import org.liveboardgames.agricola.vo.card.ActionCard;
	import org.liveboardgames.agricola.vo.farm.Farm;
	import org.liveboardgames.agricola.vo.player.Player;

	public class GameFactory
	{
		public static function createGame(players:ListCollectionView):Game
		{
			var game:Game = new Game();

			// Create the initial array of tiles
			var tiles:ListCollectionView = getTiles(players.length);
			game.tiles = tiles;

			// Initiate the waiting pile of action cards
			// that will be added to the board each round
			var actionCards:ListCollectionView = getActionCards();
			game.waitingActionCards = actionCards;

			GameHelper.addNextActionCard(game);
			GameHelper.addNextActionCard(game);
			GameHelper.addNextActionCard(game);
//			GameHelper.addNextActionCard(game);

			// Initialize the major improvement board
			var majorImprovements:ListCollectionView = getMajorImprovements();
			game.majorImprovements = majorImprovements;

			// Initiate the players of the game
			addFarms(players);
			game.players = players;

			game.currentPlayer = Player(players.getItemAt(0));
			game.hasPlayerPlayedPeep = true;

			return game;
		}

		private static function addFarms(players:ListCollectionView):void
		{
			for (var i:int = 0; i < players.length; i++)
			{
				var player:Player = Player(players.getItemAt(i));

				// Create the initial farm
				var initialFarm:Farm = FarmFactory.getInitialFarm(player);
//                initialFarm.addResource(ResourceTypeEnum.CLAY, 2);
//                initialFarm.addResource(ResourceTypeEnum.STONE, 1);
//                initialFarm.addResource(ResourceTypeEnum.GRAIN, 2);
//                initialFarm.addResource(ResourceTypeEnum.WOOD, 13);
                initialFarm.addResource(ResourceTypeEnum.REED, 4);
				if (i != 0)
				{
					initialFarm.addResource(ResourceTypeEnum.FOOD, 1);
				}
				initialFarm.addResource(ResourceTypeEnum.FOOD, 2);
//                initialFarm.addPlayedCard(MajorImprovementCards.FIREPLACE_CHEAP);
//                initialFarm.addPlayedCard(MajorImprovementCards.CLAY_OVEN);
//                initialFarm.addPlayedCard(MajorImprovementCards.STONE_OVEN);
                initialFarm.addPlayedCard(MajorImprovementCards.BASKETMAKER_WORKSHOP);
                initialFarm.addPlayedCard(MajorImprovementCards.JOINERY);
				player.farm = initialFarm;
			}
		}

		private static function getTiles(numberOfPlayers:int):ListCollectionView
		{
			// Create the initial array of tiles
			var tiles:ListCollectionView = new ArrayCollection();
			for (var i:int = 0; i < 30; i++)
			{
				var boardTile:BoardTile = new BoardTile();
				boardTile.id = i;
				// Indicate the type for the Season tiles, i.e. the 
				// tiles that get recovered little by little
				if (i >= 16)
				{
					boardTile.type = BoardTileTypeEnum.SEASON;
				}
				tiles.addItem(boardTile);
			}

			// Then defines action cards for each of the starting tiles
			// Init the action cards to put on the boards
			var actionCards:ListCollectionView = ActionCardFactory.getActionCards(numberOfPlayers);

			// Link a card to each of the tiles
			for (var j:int = 0; j < actionCards.length; j++)
			{
				var actionCard:ActionCard = actionCards.getItemAt(j) as ActionCard;
				var currentTile:BoardTile = tiles.getItemAt(j) as BoardTile;
				currentTile.actionCard = actionCard;
			}

			// Sets up the cards for the static part of the board
			var staticCards:ListCollectionView = ActionCardFactory.getStaticActionCards(GameTypeEnum.FAMILY);
			for (var k:int = 0; k < staticCards.length; k++)
			{
				var staticCard:ActionCard = staticCards.getItemAt(k) as ActionCard;
				var staticTile:BoardTile = tiles.getItemAt(k + 6) as BoardTile;
				staticTile.actionCard = staticCard;
			}

			return tiles;
		}

		private static function getActionCards():ListCollectionView
		{
			var actionCards:ListCollectionView = new ArrayCollection();

			// Add the cards of each period
			for (var i:int = 1; i <= 6; i++)
			{
				var periodCards:ListCollectionView = ActionCardFactory.getRandomCardsForPeriod(i);
				// Add each card to the list
				for each (var actionCard:ActionCard in periodCards)
				{
					actionCards.addItem(actionCard);
				}
			}
			return actionCards;
		}

		private static function getMajorImprovements():ListCollectionView
		{
			var majorImprovements:ListCollectionView = MajorImprovementFactory.getMajorImprovements();
			return majorImprovements;
		}
	}
}